package com.entertainment.ticketing.service;

import com.entertainment.ticketing.dto.TicketInfoDTO;
import com.entertainment.ticketing.entity.TicketInfo;
import com.entertainment.ticketing.repository.TicketInfoRepository;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class TicketService {
    private final TicketInfoRepository ticketRepo;

    public TicketService(TicketInfoRepository ticketRepo) {
        this.ticketRepo = ticketRepo;
    }

    public List<TicketInfoDTO> listByEvent(Long eventId, Byte status) {
        List<TicketInfo> tickets = (status == null)
                ? ticketRepo.findByEvent_Id(eventId)
                : ticketRepo.findByEvent_IdAndStatus(eventId, status);
        return tickets.stream().map(this::toDTO).collect(Collectors.toList());
    }

    private TicketInfoDTO toDTO(TicketInfo t) {
        return TicketInfoDTO.builder()
                .id(t.getId())
                .eventId(t.getEvent().getId())
                .ticketType(t.getTicketType())
                .price(t.getPrice())
                .seatArea(t.getSeatArea())
                .seatRow(t.getSeatRow())
                .seatNumber(t.getSeatNumber())
                .status(t.getStatus())
                .build();
    }
}